/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.io.Serializable;
import java.util.ArrayList;

public class Spell implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<SpellProperty> spellProperties;
	private ArrayList<String> classCastingLevels;
	//Determines if the spell should even be read from the list as part the character's current spells
	private boolean isActive;

	//The name of the spell stored here for easy access
	private String name;

	//The school of the spell.
	private String school;

	//The level of the spell for the current character
	private int level;

	// Constructor
	public Spell() {
		super();
		this.spellProperties = new ArrayList<Spell.SpellProperty>();
		this.isActive = false;
		this.setClassCastingLevels(new ArrayList<String>());
	}

	public void addClassCastingLevel(String castingLevel) {
		classCastingLevels.add(castingLevel);
	}

	public String getSpellPropertyFromIndex(int index) {
		return spellProperties.get(index).value;
	}

	public String getConcatinatedHtmlProperty(int index) {
		String retStr;
		SpellProperty sp = spellProperties.get(index);
		if (sp.getProperty().length() < 1) {
			retStr = sp.getValue() + "<br />";
		} else
			retStr = "<b>" + sp.getProperty() + "</b>" + ": " + sp.getValue() + "<br />";
		return retStr;
	}

	/**
	 * @return the spellProperties
	 */
	public ArrayList<SpellProperty> getSpellProperties() {
		return spellProperties;
	}

	public void addSpellProperty(String property, String value) {
		spellProperties.add(new SpellProperty(property, value));
	}

	/**
	 * Gets the spell level for the spell depending on the character that is
	 * using it. Wil return a -1 if a level for that hcaracter is not found
	 * 
	 * @param charClass
	 * @return
	 */
	public int getSpellLevel(int charClass) {
		String characterAbrev = ConstantHelper.getClassNameAbreviation(charClass);
		int returnValue = -1;
		for (String s : classCastingLevels) {
			String[] tempString = s.trim().split("\\s+");
			if (characterAbrev.contains(tempString[0])) {
				returnValue = Integer.parseInt(tempString[1]);
				break;
			}
		}
		return returnValue;
	}

	/**
	 * Sets the level of the spell based on the class of character who is using
	 * it.
	 * 
	 * @param charClass
	 */
	public void setCharacterSpellLevel(int charClass) {
		String characterAbrev = ConstantHelper.getClassNameAbreviation(charClass);
		for (String s : classCastingLevels) {
			String[] tempString = s.trim().split("\\s+");
			if (characterAbrev.contains(tempString[0])) {
				level = Integer.parseInt(tempString[1]);
				break;
			}
		}
	}

	/**
	 * @param spellProperties
	 *            the spellProperties to set
	 */
	public void setSpellProperties(ArrayList<SpellProperty> spellProperties) {
		this.spellProperties = spellProperties;
	}

	/**
	 * @return the isActive
	 */
	public boolean isActive() {
		return isActive;
	}

	/**
	 * @param isActive
	 *            the isActive to set
	 */
	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public void setClassCastingLevels(ArrayList<String> classCastingLevels) {
		this.classCastingLevels = classCastingLevels;
	}

	public ArrayList<String> getClassCastingLevels() {
		return classCastingLevels;
	}

	public ArrayList<String> getClassCastingLevelsBasedOnClass(String charClass) {
		return classCastingLevels;
	}

	public String getName() {
		return spellProperties.get(0).value;
	}

	public void setSpellName(String spellName) {
		this.name = spellName;
	}

	public String getSpellName() {
		return name;
	}

	class SpellProperty implements Serializable {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		String property;
		String value;

		public SpellProperty(String property, String value) {
			super();
			this.property = property;
			this.value = value;
		}

		/**
		 * @return the property
		 */
		public String getProperty() {
			return property;
		}

		/**
		 * @param property
		 *            the property to set
		 */
		public void setProperty(String property) {
			this.property = property;
		}

		/**
		 * @return the value
		 */
		public String getValue() {
			return value;
		}

		/**
		 * @param value
		 *            the value to set
		 */
		public void setValue(String value) {
			this.value = value;
		}
	}

	/**
	 * @return the spellSchool
	 */
	public String getSpellSchool() {
		return school;
	}

	/**
	 * @param spellSchool
	 *            the spellSchool to set
	 */
	public void setSpellSchool(String spellSchool) {
		this.school = spellSchool;
	}

	/**
	 * @return the spellLevel
	 */
	public int getSpellLevel() {
		return level;
	}

	/**
	 * @param spellLevel
	 *            the spellLevel to set
	 */
	public void setSpellLevel(int spellLevel) {
		this.level = spellLevel;
	}

}